// pages/tangshi/tangshi.js
const db = wx.cloud.database()

Page({
  data: {
    title: '',
    author: '',
    content: [],
    annotation: [],
    translation: [],
    note: [],
    TabCur: 0,
    scrollLeft: 0
  },
  onLoad: function () {
    wx.stopPullDownRefresh()
    this.queryData()
  },
  queryData() {
    db.collection("tangshi")
      .aggregate()
      .sample({
        size: 1
      })
      .end()
      .then(res => {
        if (res.list.length > 0) {
          this.setData({
            title: res.list[0].title,
            author: res.list[0].author,
            content: res.list[0].content,
            annotation: res.list[0].annotation,
            translation: res.list[0].translation,
            note: res.list[0].annotation,
            TabCur: 0,
            scrollLeft: 0
          })
        }
      })
      .catch(err => {
        wx.showToast({ title: '加载失败', mask: true, icon: 'error' })
        console.error('查询失败', err)
      })
  },
  onPullDownRefresh: function () {
    this.onLoad()
  },
  tabSelect(e) {
    let getNote = e.currentTarget.dataset.id === '0' ? this.data.annotation : this.data.translation
    if (e.currentTarget.dataset.id) {
      this.setData({
        TabCur: e.currentTarget.dataset.id,
        scrollLeft: (e.currentTarget.dataset.id - 1) * 60,
        note: getNote
      })
    }
  },
  onShareAppMessage: function () {
    return {
      title: '熟读唐诗三百首，不会作诗也会吟。',
      // path: '/pages/index/index',//这里是被分享的人点击进来之后的页面
    }
  }
})